import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { HttpClientModule } from '@angular/common/http';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { NZ_I18N } from 'ng-zorro-antd/i18n';
import { en_US } from 'ng-zorro-antd/i18n';
import { registerLocaleData } from '@angular/common';
import en from '@angular/common/locales/en';
import { TermComponent } from './components/tab/term/term.component';
import { AngularSplitModule } from 'angular-split';

import { NzLayoutModule } from 'ng-zorro-antd/layout';
import { NzIconModule } from 'ng-zorro-antd/icon';
import { NzMenuModule } from 'ng-zorro-antd/menu';
import { NzTabsModule } from 'ng-zorro-antd/tabs';
import { NzModalModule } from 'ng-zorro-antd/modal';
import { NzTreeModule } from 'ng-zorro-antd/tree';
import { NzTreeViewModule } from 'ng-zorro-antd/tree-view';
import { NzFormModule } from 'ng-zorro-antd/form';
import { NzDropDownModule } from 'ng-zorro-antd/dropdown';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzDividerModule } from 'ng-zorro-antd/divider';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzTableModule } from 'ng-zorro-antd/table';
import { NzSpinModule } from 'ng-zorro-antd/spin';
import { NzMessageModule } from 'ng-zorro-antd/message';
import { NzCardModule } from 'ng-zorro-antd/card';
import { NzBadgeModule } from 'ng-zorro-antd/badge';
import { NzPopconfirmModule } from 'ng-zorro-antd/popconfirm';
import { NzBreadCrumbModule } from 'ng-zorro-antd/breadcrumb';
import { NzPageHeaderModule } from 'ng-zorro-antd/page-header';
import { NzAvatarModule } from 'ng-zorro-antd/avatar';
import { NzDrawerModule } from 'ng-zorro-antd/drawer';
import { NzSwitchModule } from 'ng-zorro-antd/switch';
import { NzCheckboxModule } from 'ng-zorro-antd/checkbox';
import { NzGridModule } from 'ng-zorro-antd/grid';
import { NzSelectModule } from 'ng-zorro-antd/select';
import { NzInputNumberModule } from 'ng-zorro-antd/input-number';
import { NzProgressModule } from 'ng-zorro-antd/progress';

import { SftpComponent } from './components/tab/sftp/sftp.component';
import { FilelistComponent } from './components/tab/sftp/filelist/filelist.component';
import { WinOperationDirective } from './directive/win-operation.directive';
import { HomeComponent } from './components/home/home.component';
import { SettingComponent } from './components/home/setting/setting.component';
import { HistoryComponent } from './components/home/history/history.component';
import { HostcfgComponent } from './components/home/hostcfg/hostcfg.component';
import { HostcardComponent } from './components/home/hostcfg/hostcard/hostcard.component';
import { ForwardComponent } from './components/tab/forward/forward.component';
import { BaseTermTabComponent } from './components/tab/base-term-tab/base-term-tab.component';
import { MyTabView, MyTabPanel } from './components/public/tabview/tabview.component';
import { APP_INITIALIZER } from '@angular/core';
import { ThemeService } from './service/theme.service';
import { DragDropModule } from '@angular/cdk/drag-drop';
import { HostTreeComponent } from './components/public/host-tree/host-tree.component';
import { HostConfigService, SettingService, WinForwardConfigService } from './service/config.service';
import { ColorPickerModule } from 'ngx-color-picker';
import { LogService } from './service/log.service';
import { WinForwardComponent } from './components/home/win-forward/win-forward.component';
import { MainComponent } from './components/main/main.component';
import { SingleTermComponent } from './components/tab/term/single-term/single-term.component';
registerLocaleData(en);

export const AppInitializerProvider = {
  provide: APP_INITIALIZER,
  useFactory: (forwardService: WinForwardConfigService, settingService: SettingService, hostConfigService: HostConfigService, themeService: ThemeService, logger: LogService) => () => {
    logger.info(JSON.stringify('load host config...'))
    return hostConfigService.loadConfig()
      .then(() => {
        logger.info(JSON.stringify('load setting...'))
        return settingService.loadConfig();
      })
      .then(() => {
        logger.info(JSON.stringify('load forwarding config...'))
        return forwardService.loadConfig();
      })
      .then(() => {
        logger.info(JSON.stringify('load theme...'))
        let setting = settingService.getConfig();
        logger.info(JSON.stringify(setting, null, 4));
        themeService.setThemeMode(setting.global.darkMode);
        return themeService.loadTheme();
      });
  },
  deps: [WinForwardConfigService, SettingService, HostConfigService, ThemeService, LogService],
  multi: true
};

@NgModule({
  declarations: [
    AppComponent,
    TermComponent,
    HostcfgComponent,
    SftpComponent,
    FilelistComponent,
    WinOperationDirective,
    HistoryComponent,
    HomeComponent,
    SettingComponent,
    HostcardComponent,
    ForwardComponent,
    BaseTermTabComponent,
    MyTabView, MyTabPanel, HostTreeComponent, WinForwardComponent, MainComponent, SingleTermComponent
  ],
  imports: [
    BrowserModule,
    FormsModule, ReactiveFormsModule,
    HttpClientModule,
    BrowserAnimationsModule,
    AngularSplitModule,
    DragDropModule, ColorPickerModule,
    NzLayoutModule, NzIconModule, NzMenuModule, NzTabsModule, NzModalModule, NzTreeModule, NzTreeViewModule, NzSpinModule,
    NzFormModule, NzDropDownModule, NzInputModule, NzDividerModule, NzButtonModule, NzTableModule,
    NzMessageModule, NzCardModule, NzBadgeModule, NzPopconfirmModule, NzBreadCrumbModule, NzPageHeaderModule,
    NzAvatarModule, NzDrawerModule, NzSwitchModule, NzCheckboxModule, NzGridModule, NzSelectModule, NzInputNumberModule,
    NzProgressModule
  ],
  providers: [/*AppInitializerProvider,*/ { provide: NZ_I18N, useValue: en_US }],
  bootstrap: [AppComponent]
})
export class AppModule { }
